﻿using Anke.SHManage.Utility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Anke.SHManage.MSSQLDAL.FixedAssetsDAL
{
    /// <summary>
    /// 固定资产分类 DAL层
    /// plq
    /// </summary>
    public class FixedAssetsClassDAL
    {
        /// <summary>
        /// 新增固定资产分类
        /// </summary>
        /// <param name="PID"></param>
        /// <param name="mName"></param>
        /// <returns></returns>
        public bool SaveAssetsType(string PID, string mName)
        {
            bool result = true;
            StringBuilder sb = new StringBuilder();
            int num = GetMaxID("TDictionary") + 1; //获取表当前编码最大值，并+1
            sb.Append(" insert into TDictionary values (");
            sb.Append(" 'AssetsType-" + num + "', ");
            sb.Append(" '" + mName + "', ");
            sb.Append(" 'AssetsType', ");
            sb.Append(" " + num + ", ");
            sb.Append(" 1, ");
            sb.Append(" '" + PID + "' ");

            sb.Append(" )");

            try
            {
                int intRow = SqlHelper.ExecuteNonQuery(SqlHelper.MainConnectionString, CommandType.Text, sb.ToString(), null);
                return intRow > 0;
            }
            catch (Exception ex)
            {
                LogUtility.Error("FixedAssetsClassDAL/SaveAssetsType", ex.Message);
                result = false;
            }
            return result;
        }

        /// <summary>
        /// 获取表某字段最大值
        /// </summary>
        /// <param name="tablename">表名</param>
        /// <returns></returns>
        private int GetMaxID(string tablename)
        {
            int result = 0;
            StringBuilder sb = new StringBuilder();
            sb.Append(@" select SN from " + tablename + " where SN in (select max(cast(SN as int)) from " + tablename + " where TypeID='AssetsType' ) ");
            using (SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.MainConnectionString, CommandType.Text, sb.ToString(), null))
            {
                if (dr.Read())
                {
                    result = Convert.ToInt32(dr["SN"]);
                }
                return result;
            }
        }

        /// <summary>
        /// 修改资产分类名称
        /// </summary>
        /// <param name="MID"></param>
        /// <param name="mName"></param>
        /// <returns></returns>
        public bool EditAssetsType(string MID, string mName)
        {
            bool result = true;
            StringBuilder sb = new StringBuilder();
            sb.Append(" update TDictionary set ");
            sb.Append(@" 
                       Name='" + mName + "'");
            sb.Append(@" 
                        where 1=1 ");
            WhereClauseUtility.AddStringEqual("ID", MID, sb);
            try
            {
                int intRow = SqlHelper.ExecuteNonQuery(SqlHelper.MainConnectionString, CommandType.Text, sb.ToString(), null);
                return intRow > 0;
            }
            catch (Exception ex)
            {
                LogUtility.Error("FixedAssetsClassDAL/EditAssetsType", ex.Message);
                result = false;
            }
            return result;
        }

        /// <summary>
        /// 禁用资产分类
        /// </summary>
        /// <param name="MID"></param>
        /// <returns></returns>
        public bool DisableAssetsType(string MID)
        {
            bool result = true;
            StringBuilder sb = new StringBuilder();
            sb.Append(" update TDictionary set IsActive=0 ");
            sb.Append(@" 
                        where 1=1 ");
            WhereClauseUtility.AddStringEqual("ID", MID, sb);
            try
            {
                int intRow = SqlHelper.ExecuteNonQuery(SqlHelper.MainConnectionString, CommandType.Text, sb.ToString(), null);
                return intRow > 0;
            }
            catch (Exception ex)
            {
                LogUtility.Error("FixedAssetsClassDAL/DisableAssetsType", ex.Message);
                result = false;
            }
            return result;
        }

        /// <summary>
        /// 启用资产分类
        /// </summary>
        /// <param name="MID"></param>
        /// <returns></returns>
        public bool EnableAssetsType(string MID)
        {
            bool result = true;
            StringBuilder sb = new StringBuilder();
            sb.Append(" update TDictionary set IsActive=1 ");
            sb.Append(@" 
                        where 1=1 ");
            WhereClauseUtility.AddStringEqual("ID", MID, sb);
            try
            {
                int intRow = SqlHelper.ExecuteNonQuery(SqlHelper.MainConnectionString, CommandType.Text, sb.ToString(), null);
                return intRow > 0;
            }
            catch (Exception ex)
            {
                LogUtility.Error("FixedAssetsClassDAL/EnableAssetsType", ex.Message);
                result = false;
            }
            return result;
        }
    }
}
